from django.db import models
from mbook.models import Book
from django.contrib.auth.models import User

DEPS = (
	('A', 'CEO/CTO'),
	('B', 'DEV'),
	('C', 'OTHERS'),
)

class BookProfile(models.Model):
    '''Book status and manager'''
    book = models.OneToOneField(Book)
    num_available = models.IntegerField()
    num_reserving = models.IntegerField()
    num_total = models.IntegerField()
	
    def __unicode__(self):
        return self.book.title

    def is_available(self):
        return bool(self.num_available)
    def get_status(self):
        return self.num_available, self.num_total, self.num_reserving

class UserProfile(models.Model):
	user = models.OneToOneField(User)
	department = models.CharField(max_length=1, choices=DEPS)
	reading_books = models.ManyToManyField(BookProfile, through='Reading', related_name='books_read')
	reserving_books = models.ManyToManyField(BookProfile, through='Reserving', related_name='books_reserved')

	def __unicode__(self):
		return self.user.username

class Reading(models.Model):
	status = models.BooleanField(default=True)
	user_reading = models.ForeignKey(UserProfile)
	book_read = models.ForeignKey(BookProfile)

	def __unicode__(self):
		return self.user_reading.user.username + ' is reading ' + self.book_read.book.title

class Reserving(models.Model):
	status = models.BooleanField(default=True)
	user_reserving = models.ForeignKey(UserProfile)
	book_reserved = models.ForeignKey(BookProfile)

	def __unicode__(self):
		return self.user_reserving.user.username + ' is reserving ' + self.book_reserved.book.title
